package com.mng.index12306.framework.starter.bases;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.lang.annotation.Annotation;
import java.util.Map;

/**
 * @Description : Application Context Holder
 * @Author : YuShaochao
 * @Date : 2024-04-24 21:36
*/
public class ApplicationContextHolder implements ApplicationContextAware {
    private  static ApplicationContext CONTEXT;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextHolder.CONTEXT = applicationContext;
    }

    /**
     * Get ioc container bean by type.
     */
    public static <T> T getBean(Class<T> clazz){
        return CONTEXT.getBean(clazz);
    }

    /**
     * Get ioc container bean by name.
     */
    public static Object getBean(String name){
        return CONTEXT.getBean(name);
    }

    /**
     * Get ioc container bean by name and type.
     */
    public static <T> T getBean(String name,Class<T> clazz){
        return CONTEXT.getBean(name,clazz);
    }

    /**
     * Get a set of ioc container beans by type.
     */
    public static <T> Map<String,T> getBeansOfType(Class<T> clazz){
        return CONTEXT.getBeansOfType(clazz);
    }

    /**
     * Find whether the bean has annotations.
     */
    public static <A extends Annotation> A findAnnotationOnBean(String name,Class<A> annotationType){
        return CONTEXT.findAnnotationOnBean(name,annotationType);
    }

    /**
     * Get application context.
     */
    public static ApplicationContext getInstance(){
        return CONTEXT;
    }
}
